﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Text.RegularExpressions;
using System.Net.Mail;
using System.Net.Mime;
using System.IO;
using System.Net;

namespace BHYT.Common
{
    public class Emailer
    {
        /// <summary>
        /// Transmit an email message to a recipient without
        /// any attachments
        /// </summary>
        /// <param name="sendTo">Recipient Email Address</param>
        /// <param name="sendFrom">Sender Email Address</param>
        /// <param name="sendSubject">Subject Line Describing Message</param>
        /// <param name="sendMessage">The Email Message Body</param>
        /// <returns>Status Message as String</returns>
        public static string SendMessage(string sendTo, string sendFrom,
            string sendSubject, string sendMessage)
        {
            try
            {
                // validate the email address
                bool bTest = ValidateEmailAddress(sendTo);

                // if the email address is bad, return message
                if (bTest == false)
                    return "Invalid recipient email address: " + sendTo;

                // create the email message
                MailMessage message = new MailMessage(
                   sendFrom,
                   sendTo,
                   sendSubject,
                   sendMessage);

                // create smtp client at mail server location
                SmtpClient client = new
                SmtpClient(BHYTGlobal.SMTPNguoiDangNhap.Outgoing, BHYTGlobal.SMTPNguoiDangNhap.Port);

                // add credentials
                client.UseDefaultCredentials = true;

                // send message
                client.Send(message);

                return "Message sent to " + sendTo + " at " +
                DateTime.Now.ToString() + ".";
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }

         /// <summary>
        /// Transmit an email message with
        /// attachments
        /// </summary>
        /// <param name="sendTo">Recipient Email Address</param>
        /// <param name="sendFrom">Sender Email Address</param>
        /// <param name="sendSubject">Subject Line Describing Message</param>
        /// <param name="sendMessage">The Email Message Body</param>
        /// <param name="attachments">A string array pointing to the location  of each attachment</param>
        /// <returns>Status Message as String</returns>
        public static string SendMessageWithAttachment(string sendTo, string sendFrom, string sendSubject, string sendMessage,FileInfo fileAttachment)
        {
            //try
            //{
                // validate email address
                //bool bTest = ValidateEmailAddress(sendTo);
                //if (bTest == false)
                //    return "Invalid recipient email address: " + sendTo;
 
                // create smtp client at mail server location
                SmtpClient client = new SmtpClient(BHYTGlobal.SMTPNguoiDangNhap.Outgoing, BHYTGlobal.SMTPNguoiDangNhap.Port);
                client.EnableSsl = true;
                client.DeliveryMethod = SmtpDeliveryMethod.Network;
                client.Timeout = 20000;
                NetworkCredential objCredentials = new NetworkCredential(BHYTGlobal.NguoiDungDangNhap.Email, "201529963");
                client.Credentials = objCredentials;
                //client.UseDefaultCredentials = true; //no work
                MailMessage msg = new MailMessage();
                msg.From = new MailAddress(sendFrom);
                msg.To.Add(new MailAddress(sendTo));
                msg.Subject = sendSubject;
                msg.IsBodyHtml = true;
                msg.Body = sendMessage;
                FileStream fs = new FileStream(fileAttachment.FullName, FileMode.Open, FileAccess.Read);
                Attachment attached = new Attachment(fs, MediaTypeNames.Application.Octet);
                msg.Attachments.Add(attached);

                client.Send(msg);
                return "Message sent to " + sendTo + " at " +
                DateTime.Now.ToString() + ".";
            //}
            //catch (Exception ex)
            //{
            //    return ex.Message.ToString();
            //}
        }

        /// <summary>
        /// Confirm that an email address is valid
        /// in format
        /// </summary>
        /// <param name="emailAddress">Full email address to validate</param>
        /// <returns>True if email address is valid</returns>
        public static bool ValidateEmailAddress(string emailAddress)
        {
            try
            {
                string TextToValidate = emailAddress;
                Regex expression = new Regex(@"\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}\b");

                // test email address with expression
                if (expression.IsMatch(TextToValidate))
                {
                    // is valid email address
                    return true;
                }
                else
                {
                    // is not valid email address
                    return false;
                }
            }
            catch (Exception)
            {
                throw;
            }
        }
 
    }
}
